<?php

namespace App\Http\Controllers\Api;

use App\Transformers\UserTransformer;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PragmaRX\Google2FA\Google2FA;
use Tests\Models\User;

class UsersController extends Controller
{
    public function me() {
        return $this->response->item(auth('api')->user(),new UserTransformer());
    }

    // 生成谷歌
    public function google_2fa()
    {
        $user = auth('api')->user();
        $google2fa = new Google2FA();
        $user->google2fa_secret = $google2fa->generateSecretKey();
        // 判断是否开启了为此验证，开了
        $google2fa->setAllowInsecureCallToGoogleApis(true);

        $google2fa_url = $google2fa->getQRCodeGoogleUrl(
            'YourCompany',
            $user->email,
            $user->google2fa_secret
        );
        $user->update();
        return [$google2fa_url,$user->google2fa_secret];

    }
    // 谷歌验证
    public function google_2fa_validate(Request $request)
    {
        $user = auth('api')->user();
        $google2fa = new Google2FA();
        $secret = $request->input('google2fa_secret');
        $valid = $google2fa->getCurrentOtp($user->google2fa_secret);
        // 单独拿出去对比
        if($valid === $secret) {
            return responseJson(200,[],'success');
        }else {
            return responseJson(403,[],'error');
        }
    }
    
}
